import { credit } from "../../../../js/modules/credit.js";
import { api,forms, storage, util } from '../../../../js/util';
import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
const app = getApp();
BasePage({
  pageKey:"billList",

  /**
   * 页面的初始数据
   */
  data: {
    page: {
      PageIndex: 1,
      PageSize: 20,
      keywords: "",
      sortField: "AddTime",
      sortDirect: "desc"
    },
    list: [],
    hasMoreData: true,
    account_bill: { AccountId: 0, CreditMoney: 0, EnbBalance: 0},
    Currency:'',//货币符号
    content:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_account();
   
  },
  /**
   * 获得帐号详情
   */
  get_account:function(){
    var that = this;
    credit.get_account(function (res) {
      res = res.data;
      if (res.Success) {
        that.setData({
          account_bill: res.Content
        });
        that.loadList();
      }
      that.setData({
        content:res
      })
    });
  },

  /**
   * 加载列表
   *  */
  loadList() {
    var that = this;
    var page = this.data.page;
    credit.load_list(page, that.data.account_bill.Id, function (content) {
      var hasMoreData = content.count > (page.PageIndex) * page.PageSize;
      var index = that.data.list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (page.PageIndex > 1) {
        data = that.data.list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        list: data,
        hasMoreData: hasMoreData,
        isLoadComplete: true
      });
      // console.log(that.data.list);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.page.PageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    // console.log("onReachBottom");
    if (this.data.hasMoreData) {
      var index = this.data.page.PageIndex;
      // console.log(index + 1);
      this.data.page.PageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onUnload: function (e) {
    this.data.pre_page = this.getPreData();
    this.data.pre_page.data.isBack = true;
  },
  getPreData: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage;
  }
})